#include "sr.h"

uint32_t HV5523_Buf;

void HV5523_init(void)
{
    HV_CLK_L;HV_DIN_L;HV_LE_L;
    HV5523_senddata(0x0);//POL高电平，发送0表示漏极开路输出
}

void HV5523_senddata(uint32_t val)
{
    uint32_t tmp = val;
    for(uint8_t i = 0; i < 32; i++)
    {
        if(tmp & 1)
        {
            HV_DIN_H;
        }
        else
        {
            HV_DIN_L;
        }
        tmp >>= 1;
        
        HV_CLK_H;
        HV_CLK_L;
    }
    HV_LE_H;
    HV_LE_L;
    HV_DIN_L;
}

void Display_Num(uint8_t num, bool N2_LD, bool N2_RD, bool N1_LD, bool N1_RD)
{
    HV5523_Buf = 0x0;
    uint8_t n1 = num % 10;
    uint8_t n2 = num / 10 % 10;
    switch(n1)
    {
        case 0:HV5523_Buf ^= 1 << 8;break;
        case 1:HV5523_Buf ^= 1 << 9;break;
        case 2:HV5523_Buf ^= 1 << 11;break;
        case 3:HV5523_Buf ^= 1 << 5;break;
        case 4:HV5523_Buf ^= 1 << 7;break;
        case 5:HV5523_Buf ^= 1 << 4;break;
        case 6:HV5523_Buf ^= 1 << 6;break;
        case 7:HV5523_Buf ^= 1 << 13;break;
        case 8:HV5523_Buf ^= 1 << 10;break;
        case 9:HV5523_Buf ^= 1 << 12;break;
    }
    switch(n2)
    {
        case 0:HV5523_Buf ^= 1 << 23;break;
        case 1:HV5523_Buf ^= 1 << 24;break;
        case 2:HV5523_Buf ^= 1 << 26;break;
        case 3:HV5523_Buf ^= 1 << 20;break;
        case 4:HV5523_Buf ^= 1 << 22;break;
        case 5:HV5523_Buf ^= 1 << 19;break;
        case 6:HV5523_Buf ^= 1 << 21;break;
        case 7:HV5523_Buf ^= 1 << 28;break;
        case 8:HV5523_Buf ^= 1 << 25;break;
        case 9:HV5523_Buf ^= 1 << 27;break;
    }
    if(N2_LD)
        HV5523_Buf ^= 1 << 17;
    if(N2_RD)
        HV5523_Buf ^= 1 << 18;
    if(N1_LD)
        HV5523_Buf ^= 1 << 14;
    if(N1_RD)
        HV5523_Buf ^= 1 << 15;
    
    HV5523_senddata(HV5523_Buf);
}

void Display_Time(void)
{
    // Display_Num(rtc.getHour(),1,1,0,0);
    // vTaskDelay(700);
    // Display_Num(rtc.getMinute(),0,0,1,1);
    // vTaskDelay(700);
    // Display_Num(rtc.getHour(),1,1,0,0);
    // vTaskDelay(700);
    // Display_Num(rtc.getMinute(),0,0,1,1);
    // vTaskDelay(700);
    // Display_Num(rtc.getSecond(),1,0,0,0);
    // vTaskDelay(1000);
    // Display_Num(rtc.getSecond(),0,1,0,0);
    // vTaskDelay(1000);
    // Display_Num(rtc.getSecond(),0,0,1,0);
    // vTaskDelay(1000);
    // Display_Num(rtc.getSecond(),0,0,0,1);
    // vTaskDelay(1000);
}

void Display_Voltage(void)
{
    uint16_t VB_ADC = ReadVB;
    float VB = (VB_ADC * 3.29 * 2) / 4096 + 0.4;

    VB *= 100;
    uint8_t VB_D0 = (uint16_t)VB / 100 % 10;
    uint8_t VB_D1 = (uint16_t)VB / 10 % 10;
    uint8_t VB_D2 = (uint16_t)VB % 10;
    
    Display_Num(VB_D0 * 10 + VB_D1,0,1,1,0);
    vTaskDelay(300);
    Display_Num(VB_D0 * 10 + VB_D2,0,1,0,1);
    vTaskDelay(300);
    Display_Num(VB_D0 * 10 + VB_D1,0,1,1,0);
    vTaskDelay(300);
    Display_Num(VB_D0 * 10 + VB_D2,0,1,0,1);
    vTaskDelay(300);
}

void Nixie_Fresh(void)
{
    Display_Num(9, 0, 0, 0, 0);vTaskDelay(Fresh_Delay_Time);
    Display_Num(98, 1, 0, 0, 0);vTaskDelay(Fresh_Delay_Time);
    Display_Num(87, 1, 1, 0, 0);vTaskDelay(Fresh_Delay_Time);
    Display_Num(76, 1, 1, 1, 0);vTaskDelay(Fresh_Delay_Time);
    Display_Num(65, 1, 1, 1, 1);vTaskDelay(Fresh_Delay_Time);
    Display_Num(54, 1, 1, 1, 1);vTaskDelay(Fresh_Delay_Time);
    Display_Num(43, 1, 1, 1, 1);vTaskDelay(Fresh_Delay_Time);
    Display_Num(32, 0, 1, 1, 1);vTaskDelay(Fresh_Delay_Time);
    Display_Num(21, 0, 0, 1, 1);vTaskDelay(Fresh_Delay_Time);
    Display_Num(10, 0, 0, 0, 1);vTaskDelay(Fresh_Delay_Time);
    Display_Num(0, 0, 0, 0 , 0);vTaskDelay(Fresh_Delay_Time);
}
